package com.cm.log_process.log_query.controller;


import com.cm.log_process.log_common.config.ConfigBean;
import com.cm.log_process.log_common.entity.RequestParam;
import com.cm.log_process.log_common.entity.*;
import com.cm.log_process.log_common.exp.NoIndexException;
import com.cm.log_process.log_common.exp.ParamNotMatchException;
import com.cm.log_process.log_common.annotation.OperateLog;
import com.cm.log_process.log_query.service.QueryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.IndexNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Date 2019/12/31
 * @Author 郭兆鹏
 * 作用：日志查询控制器
 */
@Slf4j
@RestController
@RequestMapping("query")
public class QueryController {


    @Autowired
    private QueryService queryService;


    /**
     * @Author 黄鹏星
     * @param param
     * 功能:通过手机号进行查询(不带时间戳)
     */
    @OperateLog(oType = "_search", oName = "通过手机号进行查询")
    @RequestMapping(value = "/byPhone", method = RequestMethod.POST)
    public Result<Log> queryByPhone(@RequestBody RequestParam param) {
        if (StringUtils.isNotEmpty(param.getIndexName())) { //如果索引名不为空
            //设置索引名
            ConfigBean.setIndexName(param.getIndexName());
            if (StringUtils.isNotEmpty(param.getPhoneNum())) {
                PageResult<Log> byPhoneNum = queryService.findByPhoneNum(param);
                return new Result<>(StatusCode.SUCCESS.getCode(), byPhoneNum, StatusCode.SUCCESS.getMessage());
            } else {
                throw new ParamNotMatchException("缺乏主要查询参数");
            }
        } else {
            throw new NoIndexException("无索引名异常");
        }
    }

    /**
     * @Author 黄鹏星
     * @param param
     * 功能:通过流水单号和电话号码查询(不带时间戳)
     */
    @OperateLog(oType = "_search", oName = "通过流水号和电话号码查询日志")
    @RequestMapping(value = "/byTransAndPhone", method = RequestMethod.POST)
    public Result<Log> queryByTransIdAndPhone(@RequestBody RequestParam param) {
        if (StringUtils.isNotEmpty(param.getIndexName())) { //如果索引名不为空
            //设置索引名
            ConfigBean.setIndexName(param.getIndexName());
            //流水号和电话号码不能为空
            if (StringUtils.isNotEmpty(param.getTransId()) && StringUtils.isNotEmpty(param.getPhoneNum())) {
                PageResult<Log> logs = queryService.findByTransIdAndPhoneNum(param);
                //返回查询结果
                return new Result<>(StatusCode.SUCCESS.getCode(), logs, "查询成功");
            } else {
                throw new ParamNotMatchException("缺少必要参数,transId或者phoneNum");
            }
        } else {
            throw new NoIndexException("缺少索引名称");
        }
    }

    /**
     * @Author 沈万龙
     * @param param
     * 功能:根据流水单号进行查询
     */
    @OperateLog(oType = "_search", oName = "只根据流水单号进行查询")
    @RequestMapping(value = "/byTransId", method = RequestMethod.POST)
    public Result<Log> byTransId(@RequestBody RequestParam param) {
        if (StringUtils.isNotEmpty(param.getIndexName())) {
            //设置索引名
            ConfigBean.setIndexName(param.getIndexName());
            if (StringUtils.isNotEmpty(param.getTransId())) {
                try {
                    PageResult<Log> byPhoneNum = queryService.findByTransId(param);
                    return new Result<>(StatusCode.SUCCESS.getCode(), byPhoneNum, StatusCode.SUCCESS.getMessage());
                } catch (IndexNotFoundException e) {
                    throw e;
                }
            } else {
                throw new ParamNotMatchException("缺乏主要查询参数");
            }
        } else {
            throw new NoIndexException("无索引名异常");
        }
    }


}
